package org.summer.lawyer.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.core.validate.AddGroup;
import org.summer.common.core.validate.EditGroup;
import org.summer.common.excel.utils.ExcelUtil;
import org.summer.common.idempotent.annotation.RepeatSubmit;
import org.summer.common.log.annotation.Log;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.web.core.BaseController;
import org.summer.lawyer.constant.SystemConstants;
import org.summer.lawyer.domain.bo.paychannel.PayChannelBo;
import org.summer.lawyer.domain.vo.paychannel.PayChannelVo;
import org.summer.lawyer.service.IPayChannelService;

import java.util.List;

/**
 * 支付通道
 *
 * @author summer
 * @date 2024-06-21
 */
@Tag(name = "后台相关接口", description = "支付通道相关接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lawyer/payChannel")
public class PayChannelController extends BaseController {

    private final IPayChannelService payChannelService;

    /**
     * 查询支付通道列表
     */
    @Operation(summary = "查询支付通道列表接口", description = "查询支付通道列表", responses = {@ApiResponse(responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:channel:list")
    @GetMapping("/list")
    public TableDataInfo<PayChannelVo> list(PayChannelBo bo, PageQuery pageQuery) {
        return payChannelService.selectPageList(bo, pageQuery);
    }

    /**
     * 导出支付通道列表
     */
    @Operation(summary = "导出支付通道列表接口", description = "导出支付通道列表")
    @SaCheckPermission("lawyer:channel:export")
    @Log(title = "支付通道", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PayChannelBo bo, HttpServletResponse response) {
        List<PayChannelVo> list = payChannelService.selectPayChannelVoList(bo);
        ExcelUtil.exportExcel(list, "支付通道", PayChannelVo.class, response);
    }

    /**
     * 获取支付通道详细信息
     *
     * @param id 主键
     */
    @Operation(summary = "获取支付通道详细信息接口", description = "查询支付通道列表", responses = {@ApiResponse(responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:channel:query")
    @GetMapping("/{id}")
    public R<PayChannelVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long id) {
        return R.ok(payChannelService.selectPayChannelVoById(id));
    }

    /**
     * 新增支付通道
     */
    @Operation(summary = "新增支付通道接口", description = "新增支付通道", responses = {@ApiResponse(responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:channel:add")
    @Log(title = "支付通道", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/savePayChannel")
    public R<Void> savePayChannel(@Validated(AddGroup.class) @RequestBody PayChannelBo bo) {
        return toAjax(payChannelService.insertByBo(bo));
    }

    /**
     * 修改支付通道
     */
    @Operation(summary = "修改支付通道接口", description = "修改支付通道", responses = {@ApiResponse(responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:channel:edit")
    @Log(title = "支付通道", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/updatePayChannel")
    public R<Void> updatePayChannel(@Validated(EditGroup.class) @RequestBody PayChannelBo bo) {
        return toAjax(payChannelService.updateByBo(bo));
    }

    /**
     * 删除支付通道
     *
     * @param ids 主键串
     */
    @Operation(summary = "删除支付通道接口", description = "删除支付通道", responses = {@ApiResponse(responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:channel:remove")
    @Log(title = "支付通道", businessType = BusinessType.DELETE)
    @DeleteMapping("/removePayChannel/{ids}")
    public R<Void> removePayChannel(@NotEmpty(message = "主键不能为空")
                                    @PathVariable Long[] ids) {
        return toAjax(payChannelService.deleteWithValidByIds(List.of(ids), true));
    }

    @SaCheckPermission("lawyer:channel:active")
    @PostMapping("/activePayChannel")
    public R<Void> activePayChannel(@RequestBody PayChannelBo bo) {
        bo.setStatus(SystemConstants.ACTIVE);
        payChannelService.changePayChannelStatus(bo);
        return R.ok();
    }

    @SaCheckPermission("lawyer:channel:inactive")
    @PostMapping("/inactivePayChannel")
    public R<Void> inactivePayChannel(@RequestBody PayChannelBo bo) {
        bo.setStatus(SystemConstants.INACTIVE);
        payChannelService.changePayChannelStatus(bo);
        return R.ok();
    }
}
